from django.contrib.auth.models import AnonymousUser
from rest_framework.request import Request

from dvadmin.system.models import SysLog
from dvadmin.utils.request_util import get_request_user, get_request_data


def SetLog(request, msg, memo=None, type=None):
    '''
        设置系统日志
        从request中获取登录用户信息
    '''
    user = get_request_user(request)
    # data = get_request_data(request)
    info = {
        'creator': user if not isinstance(user, AnonymousUser) else None,
        'info': msg,
        'memo': memo,
        'type': type,
    }
    SysLog.objects.create(**info)


def SetLogNoLogin(userId=None, msg='', type=None):
    '''
        设置系统日志
        从request中获取登录用户信息
    '''
    info = {
        'creator_id': userId,
        'info': msg,
        'type': type,
    }
    SysLog.objects.create(**info)
